package com.uenpay.im.utilities;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.uenpay.im.weight.dialog.IOSDialog;


/**
 * 分享工具类
 */
public class ShareUtil {

	private static Intent intentForward;
	private static Activity instance;
	private static String action = "";

	/**
	 * 发送短信
	 * @param activity 上下文
	 * @param phoneNum 电话号码
	 */
	public static void sendMsg(Activity activity, String phoneNum) {
		intentForward = new Intent(Intent.ACTION_SENDTO, Uri
                .parse("sms://" + phoneNum));
		action = "发送短信至 " + phoneNum;
        instance = activity;
        startIntent(intentForward);
	}

	private static void startIntent(Intent intent) {

		new IOSDialog(instance).builder().setTitle("")
				.setMsg(action)
				.setPositiveButton("确定", new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						instance.startActivity(intentForward);
					}
				}).setNegativeButton("取消", new View.OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		}).show();


	}

	/**
	 * 拨打电话
	 * @param activity 上下文
	 * @param phoneNum 电话号码
	 */
	public static void callPhone(Activity activity, String phoneNum) {
		intentForward = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
		action = "\n客服热线\n拨打 " + phoneNum + "\n";
        instance = activity;
        startIntent(intentForward);
	}

	/**
	 * 发送邮件
	 * @param activity 上下文
	 * @param address 邮箱地址
	 */
	public static void sendEmail(Activity activity, String address) {
		intentForward = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + address));
		action = "发送邮件至 " + address;
        instance = activity;
        startIntent(intentForward);
	}

	/**
	 * 访问官网
	 * @param activity 上下文
	 * @param webUrl 网址
	 */
	public static void surfWeb(Activity activity, String webUrl) {
		intentForward = new Intent(Intent.ACTION_VIEW, Uri.parse("http://" + webUrl));
		action = "\n访问 " + webUrl;
        instance = activity;
        startIntent(intentForward);
	}

//	public static String doStartApplicationWithPackageName(String packagename) {
//
//		// 通过包名获取此APP详细信息，包括Activities、services、versioncode、name等等
//		PackageInfo packageinfo = null;
//		IMApplication application = IMApplication.getInstance();
//		if (application == null) {
//			return null;
//		}
//		try {
//			packageinfo = ((application.getBaseApplication())).getAppPackageManager().getPackageInfo(packagename, 0);
//		} catch (NameNotFoundException e) {
//			return null;
//		}
//		if (packageinfo == null) {
//			return null;
//		}
//
//		// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
//		Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
//		resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
//		resolveIntent.setPackage(packageinfo.packageName);
//
//		// 通过getPackageManager()的queryIntentActivities方法遍历
//		List<ResolveInfo> resolveinfoList = application.getBaseApplication().getAppPackageManager().queryIntentActivities(resolveIntent, 0);
//
//		ResolveInfo resolveinfo = resolveinfoList.iterator().next();
//		if (resolveinfo != null) {
//			// packagename = 参数packname
//			String packageName = resolveinfo.activityInfo.packageName;
//			// 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式：packagename.mainActivityname]
//			String className = resolveinfo.activityInfo.name;
//			// LAUNCHER Intent
//			Intent intent = new Intent(Intent.ACTION_MAIN);
//			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
//			intent.addCategory(Intent.CATEGORY_LAUNCHER);
//
//			// 设置ComponentName参数1:packagename参数2:MainActivity路径
//			ComponentName cn = new ComponentName(packageName, className);
//
//			intent.setComponent(cn);
//			application.getBaseApplication().startActivity(intent);
//			return "";
//		}
//		return null;
//	}

}
